package org.example.servlet.stuinfo;

import com.google.gson.Gson;
import org.example.dao.StuinfoDao;
import org.example.entity.Stuinfo;
import org.example.util.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/add.stuinfo")
public class AddStuinfoServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String json = RequestUtil.getRequestParameterStr(req);
        Gson gson = new Gson();
        Stuinfo stuinfo = gson.fromJson(json, Stuinfo.class);

        int row = StuinfoDao.addStuinfo(stuinfo);
        if (row > 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
        PrintWriter writer = resp.getWriter();
        writer.write(json);
        // 刷新页面
        writer.flush();
    }

}
